package ink.xiaobaibai.entity;

import com.baomidou.mybatisplus.annotation.*;
import ink.xiaobaibai.common.FreeDeviceStatus;
import ink.xiaobaibai.common.RoomNumberHandler;
import ink.xiaobaibai.valited.UpdateGroup;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-07-20
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("free_ozone_device")
public class FreeOzoneDevice implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 免费臭氧机器表主键id
     */
    @TableId(value = "id", type = IdType.AUTO)
    @Null
    @NotNull(groups = UpdateGroup.class)
    private Integer id;

    /**
     * 设别码(oz开头)
     */
    @TableField("device_code")
    @NotNull
    @NotNull(groups = UpdateGroup.class)
    private String deviceCode;

    /**
     * 管理者id
     */
    @TableField("admin_user_id")
//    @Null
//    @Null(groups = UpdateGroup.class)
    private Integer adminUserId;

    /**
     * 管理者部门id
     */
    @TableField("dep_id")
    @Null
    @Null(groups = UpdateGroup.class)
    private Integer depId;

    /**
     * 安装地址
     */
    @TableField("address")
    @NotNull
    @NotNull(groups = UpdateGroup.class)
    private String address;

    /**
     * 设备状态：空闲,运行
     */
    @TableField("status")
    @Null
    @Null(groups = UpdateGroup.class)
    private String status;

    @TableField("work_stop_time")
    @Null
    @Null(groups = UpdateGroup.class)
    private Date workStopTime;

    /**
     * 关联制氧机id
     */
    @TableField("oxygen_id")
    @NotNull(message = "请填写关联的氧气机")
    @NotNull(groups = UpdateGroup.class, message = "请填写关联的氧气机")
    private Integer oxygenId;

    /**
     * 省市
     */
    @TableField("province_city")
    @NotNull
    @NotNull(groups = UpdateGroup.class)
    private String provinceCity;

    /**
     * 启动延时时间(秒)
     */
    @TableField("delayed_time")
    @NotNull
    @Min(1)
    @NotNull(groups = UpdateGroup.class)
    @Min(groups = UpdateGroup.class, value = 1)
    private Integer delayedTime;

    /**
     * 风扇延时时间
     */
    @TableField("fan_time")
    @NotNull
    @Min(1)
    @NotNull(groups = UpdateGroup.class)
    @Min(groups = UpdateGroup.class, value = 1)
    private Integer fanTime;

    /**
     * 阀门延时时间
     */
    @TableField("valve_time")
    @NotNull
    @Min(1)
    @NotNull(groups = UpdateGroup.class)
    @Min(groups = UpdateGroup.class, value = 1)
    private Integer valveTime;

    /**
     * 对应房间号(json串)
     */
    @TableField(value = "room_number", typeHandler = RoomNumberHandler.class)
    @NotNull
    @Size(min = 1, message = "至少填写一个房间号")
    private List<String> roomNumber;

    /**
     * 上面里面的单次的房间号json
     */
    @TableField(value = "single_room_number", typeHandler = RoomNumberHandler.class)
    private List<String> singleRoomNumber;

    /**
     * 备份字段(机器型号)
     */
    @TableField("remarks")
    private String remarks;

    /**
     * 机器型号
     */
    @TableField("type")
    private String type;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    public String getStatus() {
        if (this.workStopTime == null) {
            return FreeDeviceStatus.FREE.getStatus();
        }
        if (System.currentTimeMillis() > this.workStopTime.getTime()) {
            return FreeDeviceStatus.FREE.getStatus();
        }
        return FreeDeviceStatus.INUSE.getStatus();
    }

}
